#include<iostream>
#include"pthread_moudle.hpp"
using namespace pthread_moudle;
//公共资源，票数
int g_tickets=100;


class ThreadDate
{
    ThreadDate(std::string name,int& tickets)
    :_name(name),_tickets(tickets),_self_ticket(0)
    {
    }

    ~ThreadDate()
    {}
private:
    std::string _name;
    int _self_ticket;
    int& _tickets;

};

void routine(int& tickets)
{
    while(true)
    {
        if(tickets>0)
        {
            usleep(1000);
            printf("get ticket:%d\n",tickets);
            tickets--;
        }
        else
        {
            break;
        }
    }
}
int main(int argc,char* argv[])
{
    if(argc!=2)
    {
        std::cout<<"please enter right parameter!"<<std::endl;
        return -1;
    }
    int num=std::stoi(argv[1]);
    //创建一批线程
    std::vector<Thread<int>> threads;
    for(int i=0;i<num;i++)
    {
        std::string name="Thread---"+std::to_string(i+1);
        threads.emplace_back(routine,g_tickets,name);
    
    }
    std::cout<<threads.size()<<std::endl;

    //启动线程
    for(auto &thread:threads)
    {

        bool flag=thread.start(); 
        if(flag)
        std::cout<<"启动成功"<<std::endl;
        else
        std::cout<<"启动失败"<<std::endl;  
        
    }

    //等待线程
      for(auto &thread:threads)
    {
        thread.join();  
    }
    return 0;
}